package com.example.mobile2021606128wje0506;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class SupplierActivity extends AppCompatActivity {

    private RadioGroup rdgSupplier;
    private RadioButton rbtnOne;
    private RadioButton rbtnTwo;
    private RadioButton rbtnThree;
    private RadioButton rbtnOFour;
    private Button btnSupplier;
    private TextView tvSupplierTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_supplier_activiyt);
        initView();
        btnSupplier.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //1、获取选择数据
                int radBtnID =rdgSupplier.getCheckedRadioButtonId();
                if (radBtnID!=-1){
                    RadioButton radioButton =findViewById(radBtnID);
                    String supplierName=radioButton.getText().toString();
                    //2吧数据放在intent中
                    Intent intent = new Intent();
                    startActivityForResult(intent,1);
                    intent.putExtra("supplier",radioButton.getText().toString());
                    setResult(RESULT_OK,intent);
                    finish();
               }else {
                    new AlertDialog.Builder(SupplierActivity.this)
                            .setMessage("请选择供应商")
                            .setPositiveButton("确定",null)
                            .create()
                            .show();
                }

                //3回传数据
                //4关闭Activity
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1&&resultCode==RESULT_OK){
        tvSupplierTitle.setText(data.getStringExtra("supplier"));

        }
    }

    private void initView() {
        rdgSupplier = (RadioGroup) findViewById(R.id.rdgSupplier);
        rbtnOne = (RadioButton) findViewById(R.id.rbtnOne);
        rbtnTwo = (RadioButton) findViewById(R.id.rbtnTwo);
        rbtnThree = (RadioButton) findViewById(R.id.rbtnThree);
        rbtnOFour = (RadioButton) findViewById(R.id.rbtnOFour);
        btnSupplier = (Button) findViewById(R.id.btnSupplier);
        tvSupplierTitle = (TextView) findViewById(R.id.tvSupplierTitle);
    }
}